﻿using BLL.Basis;
using Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using WebProject.Controllers.Base;

namespace WebProject.Controllers
{
    public class SystemController : Controller
    {
        SysFrameWork.SystemBLL sys_BLL;
        SysFrameWork.PowerBLL powerBLL;
        SysFrameWork.UserBLL userBLL;
        public SystemController()
        {
            sys_BLL = new SysFrameWork.SystemBLL();
            powerBLL = new SysFrameWork.PowerBLL();
            userBLL = new SysFrameWork.UserBLL();
        }
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登录首页
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult Index()
        {
            ViewData["userName"] = sys_BLL.getLoginInfo().userName;
            return View();
        }
        /// <summary>
        /// 用户列表页
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult UserList()
        {
            return View();
        }
        /// <summary>
        /// 账套配置
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult CustomBase()
        {
            return View();
        }
        /// <summary>
        /// 账套数据更新
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult UpdateCustomBase()
        {
            return View();
        }
        /// <summary>
        /// 用户编辑界面
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult EditUser()
        {
            return View();
        }
        [Authentication]
        public ActionResult ChanagePwd()
        {
            return View();
        }
        [Authentication]
        public ActionResult MenuInfo()
        {
            return View();
        }
        public ActionResult UpdateMenu()
        {
            return View();
        }
         [Authentication]
        public ActionResult ParameterInfo()
        {
            return View();
        }
         [Authentication]
        /// <summary>
        /// 权限角色界面
        /// </summary>
        /// <returns></returns>
        public ActionResult PowerRole()
        {
            return View();
        }
         [Authentication]
        /// <summary>
        /// 参数更新界面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateParameter()
        {
            return View();
        }
         [Authentication]
        public ActionResult MenuOperate()
        {
            return View();
        }
         [Authentication]
        public ActionResult UpdatePowerRole()
        {
            return View();
        }
         [Authentication]
        public ActionResult UserPower()
        {
            return View();
        }
         [Authentication]
        public ActionResult SetRolePowers()
        {
            return View();
        }
         /// <summary>
         /// 系统注册页面
         /// </summary>
         /// <returns></returns>
         public ActionResult SysAuthorized()
         {
             return View();
         }
       
       /// <summary>
       /// 系统导航菜单加载方法
       /// </summary>
       /// <returns></returns>
        public JsonResult Navigation()
        {
            return Json(sys_BLL.Navigation(""));
        }
        [HttpPost]
        public JsonResult SaveMenuInfo(string datas)
        {
            return Json(sys_BLL.SaveMenuInfo(datas));
        }
         [HttpPost]
        public JsonResult GetSysKeyCode()
        {
            return Json(sys_BLL.GetSysKeyCode());
        }
        
        [HttpPost]
        public JsonResult SaveKeyCode(string datas)
        {
            return Json(sys_BLL.SaveKeyCode(datas));
        }
        /// <summary>
        /// 权限角色更新方法
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdatePowerRole(string datas)
        {
            return Json(powerBLL.UpdatePowerRole(datas));
        }

        /// <summary>
        /// 权限角色删除方法
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DelRolePowers(int dataId)
        {
            return Json(powerBLL.DelRolePowers(dataId));
        }

        [HttpPost]
        public JsonResult getEnableMenuInfo()
        {
            return Json(sys_BLL.getEnableMenuInfo());
        }
        [HttpPost]
        public JsonResult getParameterByCode(string parCode)
        {
            return Json(sys_BLL.getParameterByCode(parCode));
        }
        [HttpPost]
        public JsonResult getRoleById(string dataId)
        {
            return Json(powerBLL.getRoleById(dataId));
        }
        [HttpPost]
        public JsonResult GetTopMenu()
        {
            return Json(sys_BLL.GetTopMenu());
        }

        [HttpPost]
        public JsonResult SaveBaseData(string datas)
        {
            return Json(sys_BLL.SaveBaseData(datas));
        }
        [HttpGet]
        public JsonResult getTableList(string page, string limit, string condition, string dataType)
        {
            var rsl = new ReturnResult();
            string pageCount = "1";
            string msg = "";
            var datas = new object();
            if (dataType == "menu")
            {
                rsl =sys_BLL.getMenuList(page, limit, ref  pageCount, condition);
            }
            else if (dataType == "cdb")
            {
                rsl = sys_BLL.CustomBaseInfo();
            }
            else if (dataType == "user")
            {
                rsl =userBLL.getUserPageList(page, limit, ref pageCount, condition);
            }
            else if (dataType == "par")
            {
                rsl = sys_BLL.getParametersList(page, limit, ref pageCount, condition);
            }
            else if (dataType == "sysPower")
            {
                rsl =sys_BLL.getSystemPowers("pt"," and status=1");
            }
             else if (dataType == "pRole")
            {
                rsl = powerBLL.getPowerRoleList(page, limit, ref pageCount, condition);
            }
            
            if (rsl.Result)
            {
                datas = rsl.Data;
            }
            else
            {
                msg = rsl.Message;
            }
            return new JsonResult()
            {
                Data = new
                {
                    count = pageCount,
                    msg = msg,
                    code = "0",
                    data = datas
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        [HttpPost]
        public JsonResult getMenuByCode(string menuCode)
        {
            return Json(sys_BLL.getMenuByCode(menuCode));
        }
        [HttpPost]
        public JsonResult saveMenuPower(string menuCode, string powerValue)
        {
            return Json(sys_BLL.saveMenuPower(menuCode, powerValue));
        }
        [HttpPost]
        public JsonResult DelMenuInfo(string dataId)
        {
            return Json(sys_BLL.DelMenuInfo(dataId));
        }
             [HttpPost]
        public JsonResult DelParmeter(string dataId)
        {
            return Json(sys_BLL.DelParmeter(dataId));
        }
        
        [HttpPost]
        public JsonResult layUiTree()
        {
            return Json(sys_BLL.layUiTree(""));
        }
        [HttpPost]
        public JsonResult getRolePowerByCode(string groupCode, string menuCode)
        {
            return Json(powerBLL.getRolePowerByCode(groupCode, menuCode));
        }
        /// <summary>
        /// 保存人员权限角色
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="groupCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveUserRole(string userCode, string groupCode)
        {
            return Json(userBLL.SaveUserRole(userCode, groupCode));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="groupCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult cleanUserRole(string userCode)
        {
            return Json(userBLL.cleanUserRole(userCode));
        }
        [HttpPost]
        public JsonResult GetWebPowers(string menuCode)
        {
            return Json(powerBLL.GetUserPower("", menuCode));
        }
        [HttpPost]
        public JsonResult SaveRolePower(string groupCode, string menuCode, string powers)
        {
            return Json(powerBLL.SaveRolePower(groupCode, menuCode, powers));
        }
        //登录方法
        [HttpPost]
        public JsonResult login()
        {
            Stream reqStream = Request.InputStream;
            StreamReader streamReader = new StreamReader(reqStream);
            var json = streamReader.ReadToEnd();
            var rsl =sys_BLL.login(json);
            if (rsl.Result)
            {
                Session["user"] = rsl.Data;
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult SavePar(string datas)
        {
            return Json(sys_BLL.SavePar(datas));
        }
        [HttpPost]
        public JsonResult getUserInfoByCode(string usCode, string dataId)
        {
            return Json(userBLL.getUserInfoByCode(usCode, dataId));
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUser(string data)
        {
            return Json(userBLL.AddUser(data));
        }
        public JsonResult RunExe(string dataType, string dataId)
        {
            var r = sys_BLL.AddSSOLogin(dataType,"", dataId);
            return Json(r);

        }
        [HttpPost]
        public JsonResult getCustomBaseByCode(string dataId)
        {
            return Json(sys_BLL.getCustomBaseByCode(dataId));
        }
        [HttpPost]
        public JsonResult reSetPwd(string userCode)
        {
            return Json(userBLL.reSetPwd(userCode));
        }
        [HttpPost]
        public JsonResult ChanagePWD(string oldPwd, string newPwd)
        {
            return Json(userBLL.ChanagePWD(oldPwd, newPwd));
        }
        [HttpPost]
        public JsonResult DelUserInfo(string userCode)
        {
            return Json(userBLL.DelUserInfo(userCode));
        }
        [HttpPost]
        public JsonResult GetTableCloums(string tCode)
        {
            return Json(sys_BLL.GetTableCloums(tCode));
        }
        public JsonResult GetDeptInfo()
        {
            DeptBLL deptBLL = new DeptBLL();
            return Json(deptBLL.GetDeptInfo());
        }
        [HttpPost]
        public JsonResult SyncUser()
        {
            return Json(userBLL.SyncUserData());
        }
        /// <summary>
        /// 验证系统是否超过服务期限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CheckInService()
        {
            return Json(sys_BLL.CheckInService());
        }
        [HttpPost]
        public JsonResult isCheckOnline()
        {
            ReturnResult rsl = new ReturnResult();
            var u = sys_BLL.getLoginInfo();
            if (u == null || string.IsNullOrWhiteSpace(u.userCode))
            {
                rsl.Result = false;
            }
            else
            {
                rsl.Result = true;
            }
            return Json(rsl);
        }
        /// <summary>
        /// 单点登录方法
        /// </summary>
        /// <returns></returns>
      
        public ActionResult SSOLogin(string userCode,string baseCode,string plCode,string gxCode,string view)
        {
            var rsl = sys_BLL.ssoLogin(userCode, baseCode);
            if (rsl.Result)
            {
                Session["user"] = rsl.Data;
            }
            Response.Redirect(sys_BLL.ToWebUrl(plCode,gxCode,view));
            return View();
        }
    }
}